﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DslamPort.Models;

namespace DslamPort.Controllers
{
    [Authorize]
    public class DslamPortController : Controller
    {
        //
        // GET: /DslamPort/
        DslamPortModel DslamPortModel = new DslamPortModel();
        DonviModel DonviModel = new DonviModel();
        public ActionResult Index()
        {
            string str = "";
            string dk = "";
            string sql = "";
            string status = "";
            int num;
            if (Session != null && Session["system_id"] != null && Session["quan_huyen_id"] != null && Session["todanpho_id"] != null)            
            {
                if (Session["select_option"].ToString() == "DslamPort.account_int")
                    dk = "a.account_int";
                if (Session["select_option"].ToString() == "DslamPort.account_mytv")
                    dk = "a.account_mytv";
                if (Session["select_status"].ToString() != "-1")
                    status = " and a.status=" + Session["select_status"].ToString();
                if (int.TryParse(Session["quan_huyen_id"].ToString(), out num))
                    str = " and a.system_id='" + Session["system_id"] + "' and c.quan_huyen_id=" + Session["quan_huyen_id"] + status;

                ViewBag.find_textbox = Session["timkiem"].ToString();
                //---------------------------------------------------
                string dk2 = "";
                if (Request["select_option"] == "DslamPort.account_int")
                    dk = "a.account_int";
                if (Request["select_option"] == "DslamPort.account_mytv")
                    dk = "a.account_mytv";

                if (Request["select_status"] == "0")
                    dk2 = " and a.status=0";
                if (Request["select_status"] == "1")
                    dk2 = " and a.status=1";
                if (Request["select_status"] == "2")
                    dk2 = " and a.status=2";
                if (ViewBag.find_textbox.Trim() == "")
                    sql =  str + " and (" + dk + " like '%" + ViewBag.find_textbox.Trim() + "%' or " + dk + " is null)" + dk2;
                else
                    sql =  str + " and (" + dk + " like '%" + ViewBag.find_textbox.Trim() + "%' or upper(" + dk + ") like upper('%" + ViewBag.find_textbox.Trim() + "%'))" + dk2;

                //-----------------------------------------------------
                ViewBag.find_selectbox = Session["select_option"];
                ViewBag.find_select_status = Session["select_status"].ToString();
                ViewBag.selectedQuan_huyen = Session["quan_huyen_id"].ToString();
                ViewBag.selectedSystem = Session["system_id"].ToString();
                ViewBag.dslam_type = Session["dslam_type"];
                
               
                ViewBag.selectedXaphuong = Session["xa_phuong_id"].ToString();
                ViewBag.selectedTodanpho = Session["todanpho_id"].ToString();
                
                //ViewBag.todanpho = new SelectList(DonviModel.getTodanpho(), "todanpho_id", "ten");
                //ViewBag.khuvuc = new SelectList(DonviModel.getKhuvuc(), "quan_huyen_id", "ten_kv");
                
                if (!int.TryParse(ViewBag.selectedQuan_huyen, out num))
                    ViewBag.donvi = new SelectList(DonviModel.getDonvi(), "quan_huyen_id", "ten_qh");
                else
                {
                    if (!int.TryParse(ViewBag.selectedXaphuong, out num))
                    {
                        ViewBag.khuvuc = new SelectList(DonviModel.getKhuvucByDonvi(ViewBag.selectedQuan_huyen), "xa_phuong_id", "ten_xp");
                    }
                    else
                    {
                        if (!int.TryParse(ViewBag.selectedTodanpho, out num))
                        {
                            ViewBag.todanpho = new SelectList(DonviModel.getTodanphoByKhuvuc(ViewBag.selectedXaphuong), "todanpho_id", "ten_tdp");
                        }
                        else
                        {

                            ViewBag.todanpho = new SelectList(DonviModel.getTodanphoByKhuvuc(ViewBag.selectedXaphuong), "todanpho_id", "ten_tdp", ViewBag.selectedTodanpho);
                        }
                        ViewBag.khuvuc = new SelectList(DonviModel.getKhuvucByDonvi(ViewBag.selectedQuan_huyen), "xa_phuong_id", "ten_xp", ViewBag.selectedXaphuong);
                    }
                    ViewBag.donvi = new SelectList(DonviModel.getDonvi(), "quan_huyen_id", "ten_qh", ViewBag.selectedQuan_huyen);
                    DslamModel dslamModel = new DslamModel();
                    
                    string s = ViewBag.selectedQuan_huyen;
                    if (int.TryParse(ViewBag.selectedXaphuong, out num))
                        s += " and a.xa_phuong_id=" + ViewBag.selectedXaphuong;
                    if (int.TryParse(ViewBag.selectedTodanpho, out num))
                        s += " and a.todanpho_id=" + ViewBag.selectedTodanpho;
                    List<DslamModel> dslam = dslamModel.getDslamByDonvi(s,1); 
                    ViewBag.dslam = new SelectList(dslam, "system_id", "name", ViewBag.selectedSystem);
                }
            }
            else
            {
                ViewBag.donvi = new SelectList(DonviModel.getDonvi(), "quan_huyen_id", "ten_qh");
            }
            List<DslamPortModel> DslamPort = DslamPortModel.getDslamPort(sql);
            
            
            return View(DslamPort);
        }
        ServiceType ServiceType = new ServiceType();
        public ActionResult EditDslamPort(int id, string system_id, string quan_huyen_id, string select_status, string select_option, string timkiem)
        {
            ViewBag.internet = ServiceType.getDv("4");
            ViewBag.mytv = ServiceType.getDv("16");
            DslamPortModel DslamPort = DslamPortModel.getDslamPort("", id.ToString()).Find(delegate(DslamPortModel s) { return Convert.ToInt32(s.id) == id; });
            if (quan_huyen_id.Length == 0)
                ViewBag.donvi = new SelectList(DonviModel.getDonvi(), "quan_huyen_id", "ten_qh");
            else
            {
                ViewBag.donvi = new SelectList(DonviModel.getDonvi(), "quan_huyen_id", "ten_qh", quan_huyen_id);
                DslamModel dslamModel = new DslamModel();
                string str = "quan_huyen_id=" + quan_huyen_id;                
                if (DslamPort.todanpho_id != "0")
                    str += " and todanpho_id="+DslamPort.todanpho_id;
                if (DslamPort.xa_phuong_id != "0")
                    str += " and xa_phuong_id=" + DslamPort.xa_phuong_id;
                List<DslamModel> dslam = dslamModel.getDslamByDonvi2(str,1);

                ViewBag.dslam = new SelectList(dslam, "system_id", "name", system_id);

                ViewBag.khuvuc = new SelectList(DonviModel.getKhuvucByDonvi(quan_huyen_id), "xa_phuong_id", "ten_xp", DslamPort.quan_huyen_id);
                ViewBag.todanpho = new SelectList(DonviModel.getTodanphoByKhuvuc(DslamPort.xa_phuong_id), "todanpho_id", "ten_tdp", DslamPort.todanpho_id);

                ViewBag.congtrong = new SelectList(DslamPortModel.getDslamPort(" and a.system_id='"+system_id+"' and a.status=0 and cong!="+DslamPort.cong+" and c.quan_huyen_id="+quan_huyen_id), "cong", "cong");
                
            }
            
            ViewBag.system_id = system_id;
            ViewBag.quan_huyen_id = quan_huyen_id;
            ViewBag.select_status = select_status;
            ViewBag.select_option = select_option;
            ViewBag.timkiem = timkiem;
            return View(DslamPort);
        }
        public ActionResult RequestAction(string id,string type)
        {
            if (DslamPortModel.checkPort2(id) > 0 && type=="bh")
            {
                TempData["error"] = "Chỉ có thể báo hỏng khi Port để trống";
                return RedirectToAction("Index");    
            }
            DslamPortModel DslamPort = DslamPortModel.getDslamPort("", id.ToString()).Find(delegate(DslamPortModel s) { return s.id == id; });
            if (DslamPort.dslam_type_id == "1")
            {
                //Dslam_API_Class.drop_ADSL_Ip(DslamPort.ip, DslamPort.slot, DslamPort.port, DslamPort.vlan);
                //Dslam_API_Class.drop_Mytv_Ip(DslamPort.ip, DslamPort.slot, DslamPort.port, DslamPort.vlan);
                //Dslam_API_Class.lock_ipDslam(DslamPort.ip, DslamPort.slot, DslamPort.port);
            }
            else if (DslamPort.dslam_type_id == "2")
            {
                //Dslam_API_Class.drop_ADSL_mini(DslamPort.ip, DslamPort.slot, DslamPort.port, DslamPort.vlan);
                //Dslam_API_Class.drop_Mytv_mini(DslamPort.ip, DslamPort.slot, DslamPort.port, DslamPort.vlan);
                //Dslam_API_Class.lock_miniDslam(DslamPort.ip, DslamPort.slot, DslamPort.port);
            }

            DslamPortModel.updateRequestAction(DslamPort,id, type);            
            TempData["msg"] = "Thông tin mới đã được cập nhật thành công!";
            return RedirectToAction("Index");        
        }

        [HttpPost]
        public ActionResult EditDslamPort(DslamPortModel DslamPort)
        {
            DslamPort.trang_thai_int = Request.Form["trang_thai_int"];
            DslamPort.hinh_thuc_int = Request.Form["hinh_thuc_int"];
            string sm_type = Request.Form["sm_type"];
            if (ModelState.IsValid)
            {
                if (sm_type != "")
                {
                    if (sm_type == "cn")
                    {
                        if (Request.Form["congtrong"] == Request.Form["cong_old"].Trim() && DslamPort.checkPort(DslamPort) > 0)
                        {
                            string quan_huyen_id = Request.Form["quan_huyen_id"];
                            TempData["error"] = "Port đã được sử dụng hoặc không tồn tại!";
                            if (quan_huyen_id.Length == 0)
                                ViewBag.donvi = new SelectList(DonviModel.getDonvi(), "quan_huyen_id", "ten_qh");
                            else
                            {
                                ViewBag.donvi = new SelectList(DonviModel.getDonvi(), "quan_huyen_id", "ten_qh", quan_huyen_id);
                                DslamModel dslamModel = new DslamModel();
                                List<DslamModel> dslam = dslamModel.getDslamByDonvi(quan_huyen_id,1);
                                ViewBag.dslam = new SelectList(dslam, "system_id", "name", DslamPort.system_id);
                            }
                            return View(DslamPort);
                        }
                    }

                    if (DslamPort.account_int != "" || DslamPort.account_mytv != "")
                        DslamPort.status = "1";
                    var checkbox = Request.Form.Get("dc_checkbox");
                    if (checkbox!=null)
                    {
                        if (DslamPort.system_id != Request.Form["system_old"].Trim() || Request.Form["congtrong"] != Request.Form["cong_old"].Trim())
                        {
                            sm_type = "dc";
                            DslamPort.cong = Request.Form["congtrong"];
                            if (DslamPort.checkPort(DslamPort, "dk2") == 0)
                            {
                                string quan_huyen_id = Request.Form["quan_huyen_id"];
                                TempData["error"] = "Port đã được sử dụng hoặc không tồn tại!";
                                ViewBag.quan_huyen_id = quan_huyen_id;
                                ViewBag.system_id = DslamPort.system_id;
                                ViewBag.system_id = DslamPort.system_id;
                                ViewBag.select_status = Request.Form["select_status"];
                                ViewBag.select_option = Request.Form["select_option"];
                                ViewBag.timkiem = Request.Form["timkiem"];
                                if (quan_huyen_id.Length == 0)
                                    ViewBag.donvi = new SelectList(DonviModel.getDonvi(), "quan_huyen_id", "ten_qh");
                                else
                                {
                                    ViewBag.donvi = new SelectList(DonviModel.getDonvi(), "quan_huyen_id", "ten_qh", quan_huyen_id);
                                    DslamModel dslamModel = new DslamModel();
                                    List<DslamModel> dslam = dslamModel.getDslamByDonvi(quan_huyen_id,1);
                                    ViewBag.dslam = new SelectList(dslam, "system_id", "name", DslamPort.system_id);

                                    ViewBag.khuvuc = new SelectList(DonviModel.getKhuvucByDonvi(quan_huyen_id), "quan_huyen_id", "ten_kv", DslamPort.quan_huyen_id);
                                    ViewBag.todanpho = new SelectList(DonviModel.getTodanphoByKhuvuc(DslamPort.quan_huyen_id), "todanpho_id", "ten", DslamPort.todanpho_id);
                                }
                                return View(DslamPort);
                            }

                        }
                    }
                    
                    if (DslamPort.account_int == null)
                    {
                        DslamPort.goidv_int = "";
                        DslamPort.hinh_thuc_int = "";
                        DslamPort.trang_thai_int = "";
                    }
                    

                    if (DslamPort.account_mytv == null)
                        DslamPort.goidv_mytv = "";
                    
                    DslamPortModel.updateDslam(DslamPort, sm_type);

                    TempData["msg"] = "Thông tin mới đã được cập nhật thành công!";
                    //return View(DslamPort);
                    return RedirectToAction("Index");
                }

            }
            TempData["error"] = "Bạn đã nhập sai thông tin!";
            //ViewBag.donvi = new SelectList(DonviModel.getDonvi(), "quan_huyen_id", "ten_qh");
            //Dslam_TypeModel Dslam_TypeModel = new Dslam_TypeModel();
            //ViewBag.dslam_type = new SelectList(Dslam_TypeModel.getDslam_Type(), "id", "name");
            return View(DslamPort);
        }

        public ActionResult FindDslamPort()
        {
            return RedirectToAction("Index");
        }
        [HttpPost]
        public ActionResult FindDslamPort(string timkiem)
        {
            //string quan_huyen_id = "";
            string system_id = "";

            string quan_huyen_id = Request.Form["quan_huyen_id"];
            ViewBag.selectedQuan_huyen = quan_huyen_id;
            string xa_phuong_id = Request.Form["xa_phuong_id"];
            ViewBag.selectedXaphuong = xa_phuong_id;
            string todanpho_id = Request.Form["todanpho_id"];
            ViewBag.selectedTodanpho = todanpho_id;
           

            int num;
            string s = "";
            ViewBag.selectedQuan_huyen = "";
            ViewBag.selectedSystem = "";
            if (int.TryParse(Request.Form["quan_huyen_id"], out num))
            {
                //quan_huyen_id = Request.Form["quan_huyen_id"];
                s += " b.quan_huyen_id=" + quan_huyen_id + " and ";
                ViewBag.selectedQuan_huyen = Request.Form["quan_huyen_id"];
            }
            if (Request.Form["system_id"] != "")
            {
                system_id = Request.Form["system_id"];
                s += " a.system_id='" + system_id + "' and ";
                ViewBag.selectedSystem = Request.Form["system_id"];
            }
            if (Request.Form["xa_phuong_id"] != "")
                s += " b.xa_phuong_id=" + xa_phuong_id + " and ";
            
            if (Request.Form["todanpho_id"] != "")
                s += " b.todanpho_id=" + todanpho_id + " and ";
            //if (int.TryParse(Request.Form["quan_huyen_id"], out num) && Request.Form["system_id"] != "")
            //    s = " c.quan_huyen_id=" + quan_huyen_id + " AND a.system_id='" + system_id + "' AND ";
            string sql;
            string dk = "";
            string dk2 = "";
            if (Request["select_option"] == "DslamPort.account_int")
                dk = "a.account_int";
            if (Request["select_option"] == "DslamPort.account_mytv")
                dk = "a.account_mytv";
            if (Request["select_option"] == "DslamPort.cong")
                dk = "a.cong";

            if (Request["select_status"] == "0")
                dk2 = " and a.status=0";
            if (Request["select_status"] == "1")
                dk2 = " and a.status=1";
            if (Request["select_status"] == "2")
                dk2 = " and a.status=2";
            ViewBag.find_select_status = Request["select_status"];

            if (timkiem.Trim() == "")
                sql = "and " + s + "(" + dk + " like '%" + timkiem.Trim() + "%' or " + dk + " is null)" + dk2;
            else
            {
                if (dk=="a.cong")
                    sql = "and " + s + " a.cong="+timkiem.Trim() + dk2;
                else
                    sql = "and " + s + "(" + dk + " like '%" + timkiem.Trim() + "%' or upper(" + dk + ") like upper('%" + timkiem.Trim() + "%'))" + dk2;
            }
            ViewBag.find_textbox = timkiem;
            ViewBag.find_selectbox = Request["select_option"];
            List<DslamPortModel> DslamPort = new List<DslamPortModel>();
            if (timkiem.Trim().Length >= 2)
            {
                DslamPort = DslamPortModel.getDslamPort(sql,"","1");
                try
                {
                    ViewBag.ip2 = "IP: " + DslamPort.Skip(0).First().ip;
                    ViewBag.system_id2 = " System_id: " + DslamPort.Skip(0).First().system_id;
                }
                catch { }
            }
            else
            {
                DslamPort = DslamPortModel.getDslamPort(sql);
            }
            ViewBag.dslam_type = 1;
            try
            {
                if (DslamPort.First().rackid == "")
                    ViewBag.dslam_type = 0;
            }
            catch { }

            string s1 = "";
            
            if (ViewBag.selectedQuan_huyen.Length == 0)
                ViewBag.donvi = new SelectList(DonviModel.getDonvi(), "quan_huyen_id", "ten_qh");
            else
            {
                if (!int.TryParse(ViewBag.selectedXaphuong, out num))
                {
                    ViewBag.khuvuc = new SelectList(DonviModel.getKhuvucByDonvi(ViewBag.selectedQuan_huyen), "xa_phuong_id", "ten_xp");
                }
                else
                {
                    if (!int.TryParse(ViewBag.selectedTodanpho, out num))
                    {
                        ViewBag.todanpho = new SelectList(DonviModel.getTodanphoByKhuvuc(ViewBag.selectedXaphuong), "todanpho_id", "ten_tdp");
                    }
                    else
                    {
                        ViewBag.todanpho = new SelectList(DonviModel.getTodanphoByKhuvuc(ViewBag.selectedXaphuong), "todanpho_id", "ten_tdp", ViewBag.selectedTodanpho);
                        s1 += " a.todanpho_id=" + ViewBag.selectedTodanpho + " and";
                    }
                    ViewBag.khuvuc = new SelectList(DonviModel.getKhuvucByDonvi(ViewBag.selectedQuan_huyen), "xa_phuong_id", "ten_xp", ViewBag.selectedXaphuong);
                    s1 += " a.xa_phuong_id=" + ViewBag.selectedXaphuong +" and";
                }
                ViewBag.donvi = new SelectList(DonviModel.getDonvi(), "quan_huyen_id", "ten_qh", ViewBag.selectedQuan_huyen);
                DslamModel dslamModel = new DslamModel();

                s1 +=   " a.quan_huyen_id=" + ViewBag.selectedQuan_huyen;
                //string s1 = ViewBag.selectedQuan_huyen;
                ////if (ViewBag.selectedQuan_huyen.Length != 0)
                //    if (int.TryParse(ViewBag.selectedQuan_huyen, out num))
                //        s1 += " and a.quan_huyen_id=" + ViewBag.selectedQuan_huyen;
                ////if (ViewBag.selectedTodanpho.Length != 0)
                //    if (int.TryParse(ViewBag.selectedTodanpho, out num))
                //        s1 += " and a.todanpho_id=" + ViewBag.selectedTodanpho;
                List<DslamModel> dslam = dslamModel.getDslamByDonvi2(s1, 1); 
                ViewBag.dslam = new SelectList(dslam, "system_id", "name", ViewBag.selectedSystem);

                try
                {
                    //DslamModel ds = DslamModel.getDslam(1, "").Find(delegate(DslamModel u) { return u.system_id == system_id; });
                    ViewBag.ip2 = "IP: " + DslamPort.Skip(0).First().ip;
                    ViewBag.system_id2 = " System_id: " + DslamPort.Skip(0).First().system_id;

                }
                catch { }
            }

            //ViewBag.todanpho = new SelectList(DonviModel.getTodanpho(), "todanpho_id", "ten");
            //ViewBag.khuvuc = new SelectList(DonviModel.getKhuvuc(), "quan_huyen_id", "ten_kv");

            Session["dslam_type"] = ViewBag.dslam_type;
            Session["system_id"] = system_id;
            Session["todanpho_id"] = todanpho_id;
            Session["quan_huyen_id"] = quan_huyen_id;
            Session["xa_phuong_id"] = xa_phuong_id;
            Session["select_status"] = Request["select_status"];
            Session["select_option"] = Request["select_option"];
            Session["timkiem"] = timkiem;
            return View("Index", DslamPort);
        }
        [HttpPost]
        public JsonResult getKhuvucJson(string quan_huyen_id, string Order)
        {
            return Json(DslamPortModel.getKhuvucJson(quan_huyen_id, Order));
        }
        [HttpPost]
        public JsonResult getTodanphoJson(string xa_phuong_id, string Order)
        {
            return Json(DslamPortModel.getTodanphoJson(xa_phuong_id, Order));
        }
        [HttpPost]
        public JsonResult getDslamJson(string quan_huyen_id, string Order)
        {
            return Json(DslamPortModel.getDslamJson(1,quan_huyen_id, Order));
        }
        [HttpPost]
        public JsonResult getDslamByKhuvucJson(string xa_phuong_id, string Order)
        {
            return Json(DslamPortModel.getDslamByKhuvucJson(1,xa_phuong_id, Order));
        }
        [HttpPost]
        public JsonResult getDslamByTodanphoJson(string todanpho_id, string Order)
        {
            return Json(DslamPortModel.getDslamByTodanphoJson(1,todanpho_id, Order));
        }
        [HttpPost]
        public JsonResult getCongtrongJson(string system_id, string system_id_old, string cong, string Order)
        {
            return Json(DslamPortModel.getCongtrongJson(system_id, system_id_old, cong, Order));
        }
        //public ActionResult DeleteDslamPort(string id)
        //{
        //    DslamPortModel.DeleteDslamPort(id);
        //    TempData["msg"] = "Thông tin mới đã được cập nhật thành công!";
        //    return RedirectToAction("Index");
        //}
        [HttpPost]
        public JsonResult getCongJson(string system_id, string Order)
        {
            return Json(DslamPortModel.getCongJson(system_id, Order));
        }
        public ActionResult Xem()
        {
            ViewBag.donvi = new SelectList(DonviModel.getDonvi("str"), "quan_huyen_id", "ten_qh");
            
            return View();
        }
        //--------------------------------------------------------------------------------------------
        Dslam_API_Class Dslam_API_Class = new Dslam_API_Class();
        public ActionResult action_in_port()
        {
            ViewBag.donvi = new SelectList(DonviModel.getDonvi(), "quan_huyen_id", "ten_qh");
            return View();
        }
        public ActionResult create_account_in_port()
        {
            ViewBag.donvi = new SelectList(DonviModel.getDonvi(), "quan_huyen_id", "ten_qh");
            return View();
        }
        public ActionResult drop_account_in_port()
        {
            ViewBag.donvi = new SelectList(DonviModel.getDonvi(), "quan_huyen_id", "ten_qh");
            return View();
        }
        [HttpPost]
        public JsonResult Lock_Unlock_PortJSon(string cong, string system_id, string status)
        {
            string result = "";
            string sql = "";
            List<DslamPortModel> DslamPort = new List<DslamPortModel>();
            sql = "and a.cong=" + cong + " and a.system_id='" + system_id + "'";
            DslamPort = DslamPortModel.getDslamPort(sql, "", "1");
            string dslam_type_id = DslamPort.First().dslam_type_id;
            string ip = DslamPort.First().ip;
            string slot = DslamPort.First().slot;
            string port = DslamPort.First().port;
            if (dslam_type_id == "1")
            {
                if (status == "0")
                    result = Dslam_API_Class.lock_ipDslam(ip, slot, port);
                else
                    result = Dslam_API_Class.Unlock_ipDslam(ip, slot, port);
            }
            else if (dslam_type_id == "2")
            {
                if (status == "0")
                    result = Dslam_API_Class.lock_miniDslam(ip, slot, port);
                else
                    result = Dslam_API_Class.unlock_miniDslam(ip, slot, port);
            }
            return Json(result);
        }
        [HttpPost]
        public JsonResult Drop_Account_In_PortJSon(string account, string cong, string system_id, string option = "")
        {
            string result = "";
            string sql = "";
            List<DslamPortModel> DslamPort = new List<DslamPortModel>();
            if (cong == "")
            {
                sql = "and (a.account_int like '%" + account.Trim() + "%' or upper(a.account_int) like upper('%" + account.Trim() + "%')) or  (a.account_mytv like '%" + account.Trim() + "%' or upper(a.account_mytv) like upper('%" + account.Trim() + "%'))";
                DslamPort = DslamPortModel.getDslamPort(sql, "", "1");
                if (DslamPort.Count != 1)
                    return Json("Phải nhập chính xác Account");
            }
            if (account == "")
            {
                sql = "and a.cong=" + cong + " and a.system_id='" + system_id + "'";
                DslamPort = DslamPortModel.getDslamPort(sql, "", "1");
            }
            string dslam_type_id = DslamPort.First().dslam_type_id;
            string ip = DslamPort.First().ip;
            string slot = DslamPort.First().slot;
            string port = DslamPort.First().port;
            string[] vlan_arr = DslamPort.First().vlan.Split('.');
            if (dslam_type_id == "1")
            {
                if (option == "4")
                    result = Dslam_API_Class.drop_ADSL_Ip(ip, slot, port, vlan_arr[0]);
                if (option == "16")
                    result = Dslam_API_Class.drop_Mytv_Ip(ip, slot, port, vlan_arr[1]);
            }
            else if (dslam_type_id == "2")
            {
                if (option == "4")
                    result = Dslam_API_Class.drop_ADSL_mini(ip, slot, port, vlan_arr[0]);
                if (option == "16")
                    result = Dslam_API_Class.drop_Mytv_mini(ip, slot, port, vlan_arr[1]);
            }
            return Json(result);
        }

        [HttpPost]
        public JsonResult Create_Account_In_PortJSon(string account, string cong, string system_id, string option = "")
        {
            string result = "";
            string sql = "";
            List<DslamPortModel> DslamPort = new List<DslamPortModel>();
            if (cong == "")
            {
                sql = "and (a.account_int like '%" + account.Trim() + "%' or upper(a.account_int) like upper('%" + account.Trim() + "%')) or  (a.account_mytv like '%" + account.Trim() + "%' or upper(a.account_mytv) like upper('%" + account.Trim() + "%'))";
                DslamPort = DslamPortModel.getDslamPort(sql, "", "1");
                if (DslamPort.Count != 1)
                    return Json("Phải nhập chính xác Account");
            }
            if (account == "")
            {
                sql = "and a.cong=" + cong + " and a.system_id='" + system_id + "'";
                DslamPort = DslamPortModel.getDslamPort(sql, "", "1");
            }

            string dslam_type_id = DslamPort.First().dslam_type_id;
            string ip = DslamPort.First().ip;
            string slot = DslamPort.First().slot;
            string port = DslamPort.First().port;
            string[] vlan_arr = DslamPort.First().vlan.Split('.');
            if (dslam_type_id == "1")
            {
                if (option == "4")
                    result += Dslam_API_Class.set_ADSL_Ip(ip, slot, port, vlan_arr[0]);
                if (option == "16")
                    result += Dslam_API_Class.set_Mytv_Ip(ip, slot, port, vlan_arr[1]);

            }
            else if (dslam_type_id == "2")
            {
                if (option == "4")
                    result += Dslam_API_Class.set_ADSL_mini(ip, slot, port, vlan_arr[0]);
                if (option == "16")
                    result += Dslam_API_Class.set_Mytv_mini(ip, slot, port, vlan_arr[1]);
            }
            
            return Json(result);
        }

        [HttpPost]
        public JsonResult Reset_Account_In_PortJSon(string account, string cong, string system_id, string option = "")
        {
            string result = "";
            string sql = "";
            List<DslamPortModel> DslamPort = new List<DslamPortModel>();
            if (cong == "")
            {
                sql = "and (a.account_int like '%" + account.Trim() + "%' or upper(a.account_int) like upper('%" + account.Trim() + "%')) or  (a.account_mytv like '%" + account.Trim() + "%' or upper(a.account_mytv) like upper('%" + account.Trim() + "%'))";
                DslamPort = DslamPortModel.getDslamPort(sql, "", "1");
                if (DslamPort.Count != 1)
                    return Json("Phải nhập chính xác Account");
            }
            if (account == "")
            {
                sql = "and a.cong=" + cong + " and a.system_id='" + system_id + "'";
                DslamPort = DslamPortModel.getDslamPort(sql, "", "1");
            }

            string dslam_type_id = DslamPort.First().dslam_type_id;
            string ip = DslamPort.First().ip;
            string slot = DslamPort.First().slot;
            string port = DslamPort.First().port;
            string[] vlan_arr = DslamPort.First().vlan.Split('.');
            if (dslam_type_id == "1")
            {
                if (option == "16")
                    result += Dslam_API_Class.reset_mytv_ip(ip, slot, port, vlan_arr[1]);

            }
            else if (dslam_type_id == "2")
            {
                if (option == "16")
                    result += Dslam_API_Class.reset_mytv_mini(ip, slot, port, vlan_arr[1]);
            }

            return Json(result);
        }
        public ActionResult do_kiem_xDSL()
        {
            ViewBag.donvi = new SelectList(DonviModel.getDonvi(), "quan_huyen_id", "ten_qh");
            return View();
        }

        [HttpPost]
        public JsonResult GetDungLuongJSon(string id, string dslam_type_id, string ip, string slot, string port)
        {
            string dung_luong = "";
            if (dslam_type_id == "1")
            {
                dung_luong = Dslam_API_Class.show_Profile_ipDslam_one_Port(ip, slot, port);
                if (dung_luong.IndexOf("DSLAM") < 0)
                {
                    DslamPortModel.updateDung_luong(id, dung_luong);
                    dung_luong = decode_DungluongIp(dung_luong);
                }
            }
            else if (dslam_type_id == "2")
            {
                dung_luong = Dslam_API_Class.show_Profile_miniDslam_one_Port(ip, slot, port);
                DslamPortModel.updateDung_luong(id, dung_luong);
            }
            return Json(dung_luong);
        }

        public JsonResult ResetDungLuongJSon(string id, string dslam_type_id, string dungluong, string ip, string slot, string port)
        {
            string result = "";
            if (dslam_type_id == "1")
            {
                result = Dslam_API_Class.reset_ipDslam(ip, decode_DungluongIp2(dungluong), slot, port);
                DslamPortModel.updateDung_luong(id, dungluong);
            }
            else if (dslam_type_id == "2")
            {
                result = Dslam_API_Class.reset_miniDslam(ip, dungluong, slot, port);
                DslamPortModel.updateDung_luong(id, dungluong);
            }
            return Json(result);
        }
        //----namtd 15/06/2016///////////////////////////
        public JsonResult ResetCongJSon(string id, string dslam_type_id, string dungluong, string ip, string slot, string port)
        {
            string result = "";
            if (dslam_type_id == "1")
            {
                result = Dslam_API_Class.reset_cong_ipDslam(ip, slot, port);
            }
            else if (dslam_type_id == "2")
            {
                result = Dslam_API_Class.reset_cong_miniDslam(ip,  slot, port);
            }
            return Json(result);
        }

        //---------------------------------------------
        public JsonResult LockPortDslamJSon(string id, string dslam_type_id, string ip, string slot, string port)
        {
            string result = "";
            if (dslam_type_id == "1")
            {
                result = Dslam_API_Class.lock_ipDslam(ip, slot, port);
                //DslamPortModel.updateDung_luong(id, decode_DungluongIp2(dungluong));
            }
            else if (dslam_type_id == "2")
            {
                result = Dslam_API_Class.lock_miniDslam(ip, slot, port);
                //DslamPortModel.updateDung_luong(id, dungluong);
            }
            return Json(result);
        }
        public JsonResult UnlockPortDslamJSon(string id, string dslam_type_id, string ip, string slot, string port)
        {
            string result = "";
            if (dslam_type_id == "1")
            {
                result = Dslam_API_Class.Unlock_ipDslam(ip, slot, port);
                //DslamPortModel.updateDung_luong(id, decode_DungluongIp2(dungluong));
            }
            else if (dslam_type_id == "2")
            {
                result = Dslam_API_Class.unlock_miniDslam(ip, slot, port);
                //DslamPortModel.updateDung_luong(id, dungluong);
            }
            return Json(result);
        }
        //Dslam_API_Class Dslam_API_Class = new Dslam_API_Class();
        public JsonResult GetSNRJSon(string account, string cong, string system_id, string option = "")
        {
            string result = "";
            string sql = "";
            List<DslamPortModel> DslamPort = new List<DslamPortModel>();
            if (cong == "")
            {
                sql = "and (a.account_int like '%" + account.Trim() + "%' or upper(a.account_int) like upper('%" + account.Trim() + "%') or  a.account_mytv like '%" + account.Trim() + "%' or upper(a.account_mytv) like upper('%" + account.Trim() + "%'))";
                DslamPort = DslamPortModel.getDslamPort(sql, "", "1");
                if (DslamPort.Count != 1)
                    return Json("Phải nhập chính xác Account");
            }
            if (account == "")
            {
                sql = "and a.cong=" + cong + " and a.system_id='" + system_id + "'";
                DslamPort = DslamPortModel.getDslamPort(sql, "", "1");
            }
            string dslam_type_id = DslamPort.First().dslam_type_id;
            string ip = DslamPort.First().ip;
            string slot = DslamPort.First().slot;
            string port = DslamPort.First().port;
            string vlan = DslamPort.First().vlan;
            /////////////////////////////////////
            result += "<input type='hidden' id='id' value='" + DslamPort.First().id + "'>";
            result += "<input type='hidden' id='dslam_type_id' value='" + DslamPort.First().dslam_type_id + "'>";
            result += "<input type='hidden' id='ip' value='" + DslamPort.First().ip + "'>";
            result += "<input type='hidden' id='slot' value='" + DslamPort.First().slot + "'>";
            result += "<input type='hidden' id='port' value='" + DslamPort.First().port + "'>";
            result += "<input type='hidden' id='cong_value' value='" + DslamPort.First().cong + "'>";
            result += "<input type='hidden' id='system_id_value' value='" + DslamPort.First().system_id + "'>";
            if (DslamPort.First().account_int.Length > 0)
                result += "Thuê bao Internet: <b>" + DslamPort.First().account_int + "</b>";
            if (DslamPort.First().account_mytv.Length > 0)
                result += "<br>Thuê bao MyTv: <b>" + DslamPort.First().account_mytv + "</b>";
            if (DslamPort.First().diachi_kh.Length > 0)
                result += "<br>Địa chỉ: <b>" + DslamPort.First().diachi_kh + "</b>";
            result += "<br>Dslam: <b>" + DslamPort.First().name + " - " + DslamPort.First().system_id + "</b>";
            /////////////////////////////////////
            if (dslam_type_id == "1")
            {
                if (option.Length < 2)
                {
                    string[] str_onePort = Dslam_API_Class.show_Profile_ipDslam_one_Port(ip, slot, port).Split(',');
                    if (str_onePort.Length == 1)
                        return Json(result + "<br><h3><i>" + str_onePort[0] + "</i></h3>");
                    result += "<br>Cổng: <b>" + DslamPort.First().cong + " - " + "Line Profile: " + decode_DungluongIp(str_onePort[0]) + ".PRF" + "</b>";
                    result += "<br>Đơn vị: ";
                    try
                    {
                        result += "<b>" + DonviModel.getTodanphoByKhuvuc(DslamPort.First().xa_phuong_id).Find(delegate(DonviModel s) { return s.todanpho_id.ToString() == DslamPort.First().todanpho_id; }).ten_tdp + "</b>";
                    }
                    catch { }
                    try
                    {
                        result += " <b>" + DonviModel.getKhuvucByDonvi(DslamPort.First().quan_huyen_id).Find(delegate(DonviModel s) { return s.xa_phuong_id.ToString() == DslamPort.First().xa_phuong_id; }).ten_xp + "</b>";
                    }
                    catch { }
                    result += " <b>" + DonviModel.getDonvi().Find(delegate(DonviModel s) { return s.quan_huyen_id.ToString() == DslamPort.First().quan_huyen_id; }).ten_qh + "</b>";
                    result += "<br>";
                    if (option.Length == 0)
                        result += str_onePort[1];
                    DslamPortModel.updateDung_luong(DslamPort.First().id, decode_DungluongIp(str_onePort[0]));
                }
                if (option.Length == 2)
                {
                    string str_onePort = Dslam_API_Class.show_Active_ipDslam_one_Port(ip, slot, port, vlan);
                    result += "<br>Cổng: <b>" + DslamPort.First().cong + "</b>";
                    result += "<br>Đơn vị: ";
                    try
                    {
                        result += "<b>" + DonviModel.getTodanphoByKhuvuc(DslamPort.First().xa_phuong_id).Find(delegate(DonviModel s) { return s.todanpho_id.ToString() == DslamPort.First().todanpho_id; }).ten_tdp + "</b>";
                    }
                    catch { }
                    try
                    {
                        result += " <b>" + DonviModel.getKhuvucByDonvi(DslamPort.First().quan_huyen_id).Find(delegate(DonviModel s) { return s.xa_phuong_id.ToString() == DslamPort.First().xa_phuong_id; }).ten_xp + "</b>";
                    }
                    catch { }
                    result += " <b>" + DonviModel.getDonvi().Find(delegate(DonviModel s) { return s.quan_huyen_id.ToString() == DslamPort.First().quan_huyen_id; }).ten_qh + "</b>";
                    result += "<br>";
                    result += str_onePort;
                }                
            }
            else if (dslam_type_id == "2")
            {
                if (option.Length < 2)
                {
                    string[] str_onePort = Dslam_API_Class.show_Profile_miniDslam_one_Port(ip, slot, port).Split(',');
                    if (str_onePort.Length == 1)
                        Json(result);
                        //return Json(str_onePort[0]);
                    result += "<br>Cổng: <b>" + DslamPort.First().cong + " - Line Profile:" + str_onePort[0] + ".PRF</b>";
                    result += "<br>Đơn vị: ";
                    try
                    {
                        result += "<b>" + DonviModel.getTodanphoByKhuvuc(DslamPort.First().xa_phuong_id).Find(delegate(DonviModel s) { return s.todanpho_id.ToString() == DslamPort.First().todanpho_id; }).ten_tdp + "</b>";
                    }
                    catch { }
                    try
                    {
                        result += "  <b>" + DonviModel.getKhuvucByDonvi(DslamPort.First().quan_huyen_id).Find(delegate(DonviModel s) { return s.xa_phuong_id.ToString() == DslamPort.First().xa_phuong_id; }).ten_xp + "</b>";
                    }
                    catch { }
                    result += "  <b>" + DonviModel.getDonvi().Find(delegate(DonviModel s) { return s.quan_huyen_id.ToString() == DslamPort.First().quan_huyen_id; }).ten_qh + "</b>";
                    result += "<br>";
                    if (option.Length == 0)
                        result += str_onePort[1];
                    DslamPortModel.updateDung_luong(DslamPort.First().id, str_onePort[0]);
                }
                if (option.Length == 2)
                {
                    string str_onePort = Dslam_API_Class.show_Active_miniDslam_one_Port(ip, slot, port, vlan);
                    result += "<br>Cổng: <b>" + DslamPort.First().cong + "</b>";
                    result += "<br>Đơn vị: ";
                    try
                    {
                        result += "<b>" + DonviModel.getTodanphoByKhuvuc(DslamPort.First().xa_phuong_id).Find(delegate(DonviModel s) { return s.todanpho_id.ToString() == DslamPort.First().todanpho_id; }).ten_tdp + "</b>";
                    }
                    catch { }
                    try
                    {
                        result += " <b>" + DonviModel.getKhuvucByDonvi(DslamPort.First().quan_huyen_id).Find(delegate(DonviModel s) { return s.xa_phuong_id.ToString() == DslamPort.First().xa_phuong_id; }).ten_xp + "</b>";
                    }
                    catch { }
                    result += " <b>" + DonviModel.getDonvi().Find(delegate(DonviModel s) { return s.quan_huyen_id.ToString() == DslamPort.First().quan_huyen_id; }).ten_qh + "</b>";
                    result += "<br>";
                    result += str_onePort;
                }

            }
            else if (dslam_type_id == "3")
            {
                result = Dslam_API_Class.SNR_msanDslam(ip, slot, port);
                //DslamPortModel.updateDung_luong(id, dungluong);
            }

            return Json(result);
        }
        public JsonResult LockMytvJSon(string dslam_type_id, string ip, string slot, string port, string vlan)
        {
            string result = "";
            string[] vlan_arr = Request.Form["vlan"].Split('.');
            if (dslam_type_id == "1")
            {
                result = Dslam_API_Class.drop_Mytv_Ip(ip, slot, port, vlan_arr[1]);
            }
            else if (dslam_type_id == "2")
            {
                result = Dslam_API_Class.drop_Mytv_mini(ip, slot, port, vlan_arr[1]);
            }
            return Json(result);
        }
        public JsonResult UnlockMytvJSon(string dslam_type_id, string ip, string slot, string port, string vlan,string goidv)
        {
            string result = "";
            string[] vlan_arr = Request.Form["vlan"].Split('.');
            if (dslam_type_id == "1")
            {
                result = Dslam_API_Class.set_Mytv_Ip(ip, slot, port, vlan_arr[1]);
                Dslam_API_Class.reset_ipDslam(ip,decode_DungluongIp2(set_Dungluong("",goidv)), slot, port);
            }
            else if (dslam_type_id == "2")
            {
                result = Dslam_API_Class.set_Mytv_mini(ip, slot, port, vlan_arr[1]);
                Dslam_API_Class.reset_miniDslam(ip, set_Dungluong("", goidv), slot, port);
            }
            return Json(result);
        }
        public JsonResult LockADSLJSon(string dslam_type_id, string ip, string slot, string port, string vlan)
        {
            string result = "";
            string[] vlan_arr = Request.Form["vlan"].Split('.');
            if (dslam_type_id == "1")
            {
                result = Dslam_API_Class.drop_ADSL_Ip(ip, slot, port, vlan_arr[0]);
            }
            else if (dslam_type_id == "2")
            {
                result = Dslam_API_Class.drop_ADSL_mini(ip, slot, port, vlan_arr[0]);
            }
            return Json(result);
        }
        public JsonResult UnlockADSLJSon(string dslam_type_id, string ip, string slot, string port, string vlan, string goidv)
        {
            string result = "";
            string[] vlan_arr = Request.Form["vlan"].Split('.');
            if (dslam_type_id == "1")
            {
                result = Dslam_API_Class.set_ADSL_Ip(ip, slot, port, vlan_arr[0]);
                Dslam_API_Class.reset_ipDslam(ip, decode_DungluongIp2(set_Dungluong(goidv,"")), slot, port);
            }
            else if (dslam_type_id == "2")
            {
                result = Dslam_API_Class.set_ADSL_mini(ip, slot, port, vlan_arr[0]);
                Dslam_API_Class.reset_miniDslam(ip, set_Dungluong(goidv, ""), slot, port);
            }
            return Json(result);
        }
        public JsonResult ping( string sys)
        {
            string sql = "";
            List<DslamPortModel> DslamPort = new List<DslamPortModel>();
            sql = " and a.system_id='" + sys + "'";
            DslamPort = DslamPortModel.getDslamPort(sql, "", "1");
           // string dslam_type_id = DslamPort.First().dslam_type_id;
            string ip = DslamPort.First().ip;
            string result = "";
            result = Dslam_API_Class.ping(ip);
            return Json(result);
        }
        public class Dslam_Obj
        {
            public string Slot { get; set; }
            public string Port { get; set; }
            public string Dung_luong { get; set; }
        }
        public string set_Dungluong(string internet_id, string mytv_id)
        {
            string dungluong = "";
            if (internet_id.Length > 0)
            {
                switch (internet_id)
                {
                    case "VNN1260":
                        dungluong = "DSL4M";
                        break;
                    case "Mega Easy":
                        dungluong = "DSL4M";
                        break;
                    case "Mega Family":
                        dungluong = "DSL4M";
                        break;
                    case "MegaVNN Extra":
                        dungluong = "DSL4M";
                        break;
                    case "Mega Maxi":
                        dungluong = "DSL4M";
                        break;
                    case "Mega Pro":
                        dungluong = "DSL4M";
                        break;
                    case "MegaVNN MaxiP":
                        dungluong = "DSL4M";
                        break;
                    case "Mega For Game":
                        dungluong = "DSL4M";
                        break;
                    case "Mega Dreaming":
                        dungluong = "DSL4M";
                        break;
                    case "Mega Basic":
                        dungluong = "DSL4M";
                        break;
                    default:
                        dungluong = "DSL4M";
                        break;
                }
            }
            if (mytv_id.Length > 0)
            {
                switch (mytv_id)
                {
                    case "MyTV":
                        dungluong = "DSL8M";
                        break;
                    case "MyTV HD":
                        dungluong = "DSL8M";
                        break;
                    case "Full Option SD":
                        dungluong = "DSL8M";
                        break;
                    case "Full Option HD":
                        dungluong = "DSL8M";
                        break;
                    case "MyTV Silver":
                        dungluong = "DSL8M";
                        break;
                    case "MyTV Silver HD":
                        dungluong = "DSL8M";
                        break;
                    case "MyTV Gold":
                        dungluong = "DSL8M";
                        break;
                    case "MyTV Gold HD":
                        dungluong = "DSL8M";
                        break;
                    default:
                        dungluong = "DSL8M";
                        break;
                }
            }
            return dungluong;
        }
        public static string decode_DungluongIp(string dungluong)
        {
            string dungluong_ip;
            switch (dungluong)
            {
                case "117":
                    dungluong_ip = "DSL4M";
                    break;
                case "118":
                    dungluong_ip = "DSL4M5";
                    break;
                case "119":
                    dungluong_ip = "DSL5M";
                    break;
                case "120":
                    dungluong_ip = "DSL6M";
                    break;
                case "121":
                    dungluong_ip = "DSL7M";
                    break;
                case "122":
                    dungluong_ip = "DSL8M";
                    break;
                case "123":
                    dungluong_ip = "DSL9M";
                    break;
                case "124":
                    dungluong_ip = "DSL10M";
                    break;
                case "125":
                    dungluong_ip = "DSL11M";
                    break;
                case "126":
                    dungluong_ip = "DSL12M";
                    break;
                case "127":
                    dungluong_ip = "DSL13M";
                    break;
                case "128":
                    dungluong_ip = "DSL14M";
                    break;
                case "129":
                    dungluong_ip = "DSL15M";
                    break;
                case "130":
                    dungluong_ip = "DSL16M";
                    break;
                case "131":
                    dungluong_ip = "DSL17M";
                    break;
                case "132":
                    dungluong_ip = "DSL18M";
                    break;
                case "133":
                    dungluong_ip = "DSL20M";
                    break;
                case "134":
                    dungluong_ip = "DSL22M";
                    break;
                default:
                    dungluong_ip = "";
                    break;
            }
            return dungluong_ip;
        }
        public string decode_DungluongIp2(string dungluong)
        {
            string dungluong_ip;
            switch (dungluong)
            {
                case "DSL4M":
                    dungluong_ip = "117";
                    break;
                case "DSL4M5":
                    dungluong_ip = "118";
                    break;
                case "DSL5M":
                    dungluong_ip = "119";
                    break;
                case "DSL6M":
                    dungluong_ip = "120";
                    break;
                case "DSL7M":
                    dungluong_ip = "121";
                    break;
                case "DSL8M":
                    dungluong_ip = "122";
                    break;
                case "DSL9M":
                    dungluong_ip = "123";
                    break;
                case "DSL10M":
                    dungluong_ip = "124";
                    break;
                case "DSL11M":
                    dungluong_ip = "125";
                    break;
                case "DSL12M":
                    dungluong_ip = "126";
                    break;
                case "DSL13M":
                    dungluong_ip = "127";
                    break;
                case "DSL14M":
                    dungluong_ip = "128";
                    break;
                case "DSL15M":
                    dungluong_ip = "129";
                    break;
                case "DSL16M":
                    dungluong_ip = "130";
                    break;
                case "DSL17M":
                    dungluong_ip = "131";
                    break;
                case "DSL18M":
                    dungluong_ip = "132";
                    break;
                case "DSL20M":
                    dungluong_ip = "133";
                    break;
                case "DSL22M":
                    dungluong_ip = "134";
                    break;
                default:
                    dungluong_ip = "117";
                    break;
            }
            return dungluong_ip;
        }
    }
}
